package org.example.hw.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

@MappedSuperclass
public abstract class AbstractEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@ManyToOne
	@JoinColumn(name = "fk_created_by_user", updatable = false)
	private User createdBy;

	@ManyToOne
	@JoinColumn(name = "fk_modified_by_user", updatable = true)
	private User modifiedBy;

	@NotNull
	@Column(nullable = false, updatable = false)
	private Date creationTime;

	@NotNull
	@Column(nullable = false, updatable = true)
	private Date modificationTime;

	@Version
	private Long version;

	@PrePersist
	public void prePersist() {
		Date now = new Date();
		creationTime = now;
		modificationTime = now;
	}

	@PreUpdate
	public void preUpdate() {
		modificationTime = new Date();
	}

	public User getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}

	public User getModifiedBy() {
		return modifiedBy;
	}

	public void setModifiedBy(User modifiedBy) {
		this.modifiedBy = modifiedBy;
	}

	public Date getCreationTime() {
		return creationTime;
	}

	public void setCreationTime(Date creationTime) {
		this.creationTime = creationTime;
	}

	public Date getModificationTime() {
		return modificationTime;
	}

	public void setModificationTime(Date modificationTime) {
		this.modificationTime = modificationTime;
	}

	public Long getVersion() {
		return version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().
				append(createdBy).
				append(modifiedBy).
				append(creationTime).
				append(modificationTime).
				hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}

		final AbstractEntity otherEntity = (AbstractEntity) obj;

		return new EqualsBuilder().
				append(createdBy, otherEntity.getCreatedBy()).
				append(modifiedBy, otherEntity.getModifiedBy()).
				append(creationTime, otherEntity.getCreationTime()).
				append(modificationTime, otherEntity.getModificationTime()).
				isEquals();
	}

	@Override
	public String toString() {

		String createdByUsername = "";
		String modifiedByUsername = "";

		if (createdBy != null) {
			createdByUsername = createdBy.getUsername();
		}

		if (modifiedBy != null) {
			modifiedByUsername = modifiedBy.getUsername();
		}

		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).
				append("createdBy", createdByUsername).
				append("modifiedBy", modifiedByUsername).
				append("creationTime", creationTime).
				append("modificationTime", modificationTime).
				toString();
	}

}
